
/*
* 从文本文件中读取题目数据，并将每个题目存储为一个Question对象，
* 然后添加到ArrayList<Question>中
*/

package ok;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

 public class QuestionList {

     //这个函数用来从文件中读取文字返回题目列表
    public ArrayList<Question> loadQuestions(String filePath) {
        ArrayList<Question> questionList = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(";");

                if (parts.length == 6) {
                    String question = parts[0];
                    String optionA = parts[1];
                    String optionB = parts[2];
                    String optionC = parts[3];
                    String optionD = parts[4];
                    String correctAnswer = parts[5];

                    Question q = new Question(question, optionA, optionB, optionC, optionD,correctAnswer);
                    questionList.add(q);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();               //在命令行打印异常信息在程序中出错的位置及原因
        }

        return questionList;
    }
}
